package com.sjzs.web.common;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sjzs.web.po.User00;
/**
 * @deprecated: Action基础类
 * @author qinj 
 * @version 1.0
 * 
 */
public class BaseAction extends ActionSupport implements ServletRequestAware,
		ServletResponseAware {
	private static final long serialVersionUID = 1L;
	protected Logger logger = Logger.getLogger(this.getClass());
	private HttpServletRequest request;
	private HttpServletResponse response;
	private User00 user00;
	/**
	 * 以流的形式获取请求参数
	 * @param request
	 * @return
	 */
	protected String getParameterString(HttpServletRequest request){
		String x = "";
		try {
			InputStreamReader reader = new InputStreamReader(request.getInputStream(), "UTF-8"); 
			char[] buff = new char[1024]; 
			int length = 0; 
			while ((length = reader.read(buff)) != -1){
				x = new String(buff, 0, length); 
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("获取请求参数报错！");
		}
		 return x;
	}

	public Logger getLogger() {
		return logger;
	}
	public void setLogger(Logger logger) {
		this.logger = logger;
	}
	public HttpServletRequest getRequest() {
		return request;
	}
	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}
	public HttpServletResponse getResponse() {
		return response;
	}
	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}
	public User00 getUser00() {
		ActionContext actionContext = ActionContext.getContext();
        Map session = actionContext.getSession();
        user00=(User00) session.get(GlobalNames.CURUSER);
		//user00=(User00) request.getSession().getAttribute(GlobalNames.CURUSER);
		return user00;
	}
	public void setUser00(User00 user00) {
		this.user00 = user00;
	}
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request=request;
		
	}
	@Override
	public void setServletResponse(HttpServletResponse response) {
	   this.response=response;
	}
	
	/**
	 * 将对象转换成JSON字符串，并响应回前台
	 * @param object
	 */
	public void writeJson(Object object) {
		try {		
			String json = JSON.toJSONStringWithDateFormat(object, "yyyy-MM-dd HH:mm:ss");
			ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
			ServletActionContext.getResponse().getWriter().write(json);
			ServletActionContext.getResponse().getWriter().flush();
			ServletActionContext.getResponse().getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
